package com.vicykie.admin.serivice;

import com.vicykie.admin.dao.LoginUserDAO;
import com.vicykie.admin.entity.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class LoginUserDetailService implements UserDetailsService {
    @Autowired
    private LoginUserDAO loginUserDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("=====获取用户信息==========" + username);
        LoginUser loginUser = loginUserDAO.findByUsername(username);
        if (loginUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("role_user"));
        return new User(username, loginUser.getPassword(), loginUser.isEnabled(), true, true, true, authorities);
    }
}
